Repository navigation
Add Case / When conditional function - #1421
Open
regiscamimura wants to merge 3 commits into
Open
regiscamimura wants to merge 3 commits into
regiscamimura wants to merge 3 commits into
Conversation
A condition may be a where clause or a QueryString, and only the first is covered - the pass-through branch is what a function-wrapped predicate takes, e.g. Upper(col).like(...).
Member
|
Good idea, thanks! |
Member
|
I like the syntax of this. The only question is this bit It seems to fail at the moment on Cockroach because of |
Contributor
Author
|
Yeah, it seems Cockroach doesn't default an untyped placeholder to Rather than extend the literal-inlining workaround, I've dropped it: every plain Python value is now a parameter wrapped in a CASE WHEN "ticket"."price" >= $1 THEN CAST($2 AS TEXT) ELSE CAST($3 AS TEXT) END |
Cockroach can't infer the type of a bare placeholder inside CASE and raises IndeterminateDatatypeError, so string THEN/ELSE values failed there. Wrap every plain Python value in a CAST keyed off its type (str/int/float/Decimal/bool), the same as Concat does, and drop the literal inlining for numbers.
regiscamimura
force-pushed
the
case-when
branch
from
September 17, 2026 20:26
d18dab0 to
1db8e24
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
piccolo.query.functions.conditionalhasCoalesceandNullIf, but there's noway to express a SQL
CASEstatement. This addsCaseandWhen, in the samestyle as the existing functions.
Because it's a
QueryString, it also works inwhereclauses andorder_by:The one non-obvious bit:
THENvalues aren't always query parametersPostgres can't infer the type of a bare parameter inside a
CASE, so it assumestext. If you bind an integer, asyncpg then rejects it before the query is evensent:
Strings happen to work, which makes it an easy trap to fall into — the obvious
example works and the second one doesn't.
So
get_case_value_stringwrites numbers, booleans andNoneinto the SQLdirectly, and parameterises everything else. There's no injection risk, as the
branch is only taken once the value is known to be a Python
int/float/Decimal/bool(non-finite floats and decimals fall back to a parameter).Alternatives I considered, in case you'd prefer one of them:
Castautomatically. Needs a Python-type → SQL-typemapping which Piccolo doesn't have today, and the guesses are lossy (is an
intanINTEGERor aBIGINT?). It's also risky on SQLite, whereCAST(? AS TIMESTAMP)would mangle a datetime.Cast's own docstring already documents this classof problem, so there's precedent — but it makes the most obvious
Caseexample fail, which felt like the wrong default.
Happy to change it either way.
Notes
defaultis optional. Omitting it omits theELSE, which is the same thingas
ELSE NULLin SQL.aliasdefaults to"case", matching howFunctionderives its defaultalias — without it Postgres and SQLite name the column differently.
Whenaccepts a where clause (Where/WhereRaw/And/Or) or aQueryString, andthen/defaultaccept a column, another function, or aplain value.